package com.whfc.fvs.service.xxl.job;

import com.whfc.XxlJobConfig;
import com.whfc.fvs.service.UniResourceSyncService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

/**
 * 宇视科技资源同步定时任务
 * 
 * @Description 定时同步浙江宇视科技有限公司的资源到本系统视频监控分组
 */
@Component
@ConditionalOnBean(XxlJobConfig.class)
public class UniResourceSyncJob {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UniResourceSyncService uniResourceSyncService;

    /**
     * 宇视科技资源同步定时任务
     */
    @XxlJob("uni-resource-sync")
    public void uniResourceSync() {
        try {
            XxlJobHelper.log("开始执行宇视科技资源同步定时任务");

            // 执行资源同步
            uniResourceSyncService.syncUniResourcesToFvs();

            XxlJobHelper.log("宇视科技资源同步定时任务执行完成");

        } catch (Exception ex) {
            logger.error("宇视科技资源同步定时任务执行失败", ex);
            XxlJobHelper.log("宇视科技资源同步定时任务执行失败 error: {}", ex.getMessage());
            XxlJobHelper.handleFail("宇视科技资源同步定时任务执行失败 error:" + ex.getLocalizedMessage());
        }
    }

    /**
     * 宇视科技资源同步手动触发任务
     * 用于手动触发资源同步
     */
    @XxlJob("uni-resource-sync-manual")
    public void uniResourceSyncManual() {
        try {
            XxlJobHelper.log("开始执行宇视科技资源同步手动触发任务");

            // 执行资源同步
            uniResourceSyncService.syncUniResourcesToFvs();

            XxlJobHelper.log("宇视科技资源同步手动触发任务执行完成");

        } catch (Exception ex) {
            logger.error("宇视科技资源同步手动触发任务执行失败", ex);
            XxlJobHelper.log("宇视科技资源同步手动触发任务执行失败 error: {}", ex.getMessage());
            XxlJobHelper.handleFail("宇视科技资源同步手动触发任务执行失败 error:" + ex.getLocalizedMessage());
        }
    }
}